package com.onlyphoto.onlymall.widget;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.OnlyDialogFragment;

import com.onlyphoto.onlymall.R;

import java.util.Objects;


/**
 * Created by gaochujia on 2019-7-1.
 */

public class CommonDialog extends OnlyDialogFragment implements View.OnClickListener {

    private ImageView successIcon;
    private TextView messageTxt, cancelBtn, okBtn, contentTxt;
    private View bottomLayout, lineView;
    private FrameLayout customLayout;

    private View customView;

    private ImageView iv_close;

    private String msgStr, cancelStr, okStr, subMsg;

    private boolean isSuccess = false, isOneBtn = false;

    private DialogClickEvent mOkEvent, mCancelEvent;
    private DialogDismissEvent mDismissEvent;

    public static CommonDialog newInstance() {
        return new CommonDialog();
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = (int) (isSuccess ? (dm.widthPixels * 0.75) : (dm.widthPixels * 0.8));
            dialog.getWindow().setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View inflateView = layoutInflater.inflate(R.layout.dialog_fragment_common, null);
        successIcon = inflateView.findViewById(R.id.icon);
        messageTxt = inflateView.findViewById(R.id.txt_msg);
        bottomLayout = inflateView.findViewById(R.id.bottom_layout);
        iv_close = inflateView.findViewById(R.id.iv_close);
        cancelBtn = inflateView.findViewById(R.id.btn_cancel);
        lineView = inflateView.findViewById(R.id.line);
        contentTxt = inflateView.findViewById(R.id.txt_content);
        okBtn = inflateView.findViewById(R.id.btn_ok);

        customLayout = inflateView.findViewById(R.id.layout_customer);

        bottomLayout.setVisibility(isSuccess ? View.GONE : View.VISIBLE);
        successIcon.setVisibility(isSuccess ? View.VISIBLE : View.GONE);

        cancelBtn.setVisibility(isOneBtn ? View.GONE : View.VISIBLE);
        lineView.setVisibility(isOneBtn ? View.GONE : View.VISIBLE);

        if (customView != null) {
            customLayout.removeAllViews();
            customLayout.addView(customView);
        } else {
            if (!TextUtils.isEmpty(msgStr)) {
                messageTxt.setVisibility(View.VISIBLE);
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    messageTxt.setText(Html.fromHtml(msgStr, Html.FROM_HTML_MODE_LEGACY));
                } else {
                    messageTxt.setText(Html.fromHtml(msgStr));
                }
            }
            if (!TextUtils.isEmpty(subMsg)) {
                contentTxt.setVisibility(View.VISIBLE);
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    contentTxt.setText(Html.fromHtml(subMsg, Html.FROM_HTML_MODE_LEGACY));
                } else {
                    contentTxt.setText(Html.fromHtml(subMsg));
                }
            }
        }

        if (!TextUtils.isEmpty(okStr)) {
            okBtn.setText(okStr);
        }

        if (!TextUtils.isEmpty(cancelStr)) {
            cancelBtn.setText(cancelStr);
        }
        if(null!=listener){
            iv_close.setVisibility(View.VISIBLE);
            iv_close.setOnClickListener(view -> {
                if (null != listener) {
                    listener.onClose();
                }
            });
        }
        okBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        builder.setView(inflateView);
        //有确定取消按钮 点击外部不消失
        AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
        return dialog;
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

        if (bottomLayout != null) {
            bottomLayout = null;
        }

        if (customView != null) {
            //消失时 清理自定义View防止内存泄露
            customView = null;
        }

        if (mDismissEvent != null) {
            mDismissEvent.onDismissDialog();
        }
    }


    /**
     * 设置自定义View
     *
     * @param view
     * @return
     */
    public CommonDialog setCustomView(View view) {
        customView = view;
        return this;
    }

    /**
     * 设置dialog类型【默认底部有两个按钮】
     *
     * @param isSuccess true ： 底部没有按钮
     * @param isOneBtn  true:底部一个按钮
     * @return
     */
    public CommonDialog setDialogType(boolean isSuccess, boolean isOneBtn) {
        this.isSuccess = isSuccess;
        this.isOneBtn = isOneBtn;
        return this;
    }

    /**
     * 设置Dialog提示内容
     *
     * @param message 提示内容
     * @return
     */
    public CommonDialog setMessage(String message) {
        msgStr = TextUtils.isEmpty(message) ? "" : message;
        return this;
    }

    /**
     * 设置Dialog提示内容
     *
     * @param message 提示内容
     * @return
     */
    public CommonDialog setSubMessage(String message) {
        subMsg = TextUtils.isEmpty(message) ? "" : message;
        return this;
    }

    /**
     * 确定按钮的显示字段
     *
     * @param str   名称
     * @param event 对应点击事件
     * @return
     */
    public CommonDialog setOkBtn(String str, @Nullable DialogClickEvent event) {
        okStr = TextUtils.isEmpty(str) ? "确定" : str;
        mOkEvent = event;
        return this;
    }

    /**
     * 取消按钮的显示字段
     *
     * @param str   名称
     * @param event 对应点击事件
     * @return
     */
    public CommonDialog setCancelBtn(String str, @Nullable DialogClickEvent event) {
        cancelStr = TextUtils.isEmpty(str) ? "取消" : str;
        mCancelEvent = event;
        return this;
    }

    /**
     * Dialog消失时 状态回调
     *
     * @param event DialogDismissEvent
     * @return
     */
    public CommonDialog setDismissEvent(DialogDismissEvent event) {
        mDismissEvent = event;
        return this;
    }

    private OnCloseCallback listener;
    /**
     * Dialog点击关闭
     *
     * @param listener 点击关闭
     * @return
     */
    public CommonDialog setCloseEvent(OnCloseCallback listener) {
        this.listener=listener;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                if (mCancelEvent != null) {
                    mCancelEvent.onClick(v);
                }
                break;

            case R.id.btn_ok:
                if (mOkEvent != null) {
                    mOkEvent.onClick(v);
                }
                break;

            default:
                break;
        }
        dismiss();
    }

    public interface DialogDismissEvent {
        void onDismissDialog();
    }

    public interface DialogClickEvent {
        void onClick(View view);
    }

    public interface OnCloseCallback {
        void onClose();
    }
}

